import Wsocket from "@/utils/Wsocket.js"
import tool from "@/utils/tools"

class Message {
  ws
  timer = null
  interval = 10 * 1000
  constructor() {
    this.ws = new Wsocket(import.meta.env.VITE_APP_WS_URL + "queue/join", {
      onOpen: (_) => {
        console.log("已成功连接到消息服务器...", _)
      },
      onError: (_) => {
        this.ws = undefined
        console.log("未成功连接到消息服务器...", _)
      },
      onClose: (_) => {
        this.ws = undefined
        console.log("与消息服务器断开...", _)
      }
    })

    this.ws.heartbeat.openHeartbeat = false
  }

  getMessage() {
    this.timer = setInterval(() => {
      this.ws && this.ws.send({ event: "message"})
    }, this.interval)
  }

  connection() {
    this.ws.connection()
  }
}

export default Message
